package com.xp.flower.qsd.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xp.flower.api.IUserAreaService;
import com.xp.flower.common.ajax.AjaxError;
import com.xp.flower.common.ajax.AjaxResponse;
import com.xp.flower.common.dto.UserAreaDTO;
import com.xp.flower.common.dto.UserDTO;
import com.xp.flower.qsd.consts.WebConsts;

@Controller
@RequestMapping("userAreaController")
public class UserAreaController {
    
    @Autowired
    private IUserAreaService userareaServiceImpl;

    @RequestMapping("goToBaseAreaManager")
    public String goToBaseAreaManager(@RequestParam(value = "userId",required=false) Long userId,Model model) {
        List<UserAreaDTO> userAreaDtoList = userareaServiceImpl.queryUserAreaByUserId(userId);
        model.addAttribute("userAreaDtoList", userAreaDtoList);
        return "/qsd/baseAreaManager";
    }
    
    @RequestMapping("goToBaseAreaEdit")
    public String goToBaseAreaEdit(@RequestParam(value = "id",required=false) Long id,Model model) {
        
        if(null != id){
            UserAreaDTO userArea = userareaServiceImpl.queryUserAreaById(id);
            model.addAttribute("userArea", userArea);
        }
        
        return "/qsd/baseAreaEdit";
    }
    
    @RequestMapping("addUserArea")
    @ResponseBody
    public AjaxResponse<UserAreaDTO> addUserArea(UserAreaDTO userAreaDTO,HttpSession session){
        UserDTO userDTO = (UserDTO)session.getAttribute(WebConsts.USERINFO);
        if(userDTO != null){
            userAreaDTO.setUserId(userDTO.getId());
        }
        
        Integer row = 0;
        if(null != userAreaDTO.getId()){
            row = userareaServiceImpl.updateUserArea(userAreaDTO);
        }else{
            row = userareaServiceImpl.addUserArea(userAreaDTO);
        }
        
        AjaxResponse<UserAreaDTO> ajaxResponse = null;
        
        
        if(null != userAreaDTO.getId()){
            if(row > 0){
                ajaxResponse = new AjaxResponse<UserAreaDTO>(AjaxError.EDITADDRESSCOMMITSUCCESS.getCode(),AjaxError.EDITADDRESSCOMMITSUCCESS.getMessage());
            }else{
                ajaxResponse = new AjaxResponse<UserAreaDTO>(AjaxError.EDITADDRESSCOMMITFAIL.getCode(),AjaxError.EDITADDRESSCOMMITFAIL.getMessage());
            }
        }else{
            if(row > 0){
                ajaxResponse = new AjaxResponse<UserAreaDTO>(AjaxError.ADDRESSCOMMITSUCCESS.getCode(),AjaxError.ADDRESSCOMMITSUCCESS.getMessage());
            }else{
                ajaxResponse = new AjaxResponse<UserAreaDTO>(AjaxError.ADDRESSCOMMITFAIL.getCode(),AjaxError.ADDRESSCOMMITFAIL.getMessage());
            }
        }
        return ajaxResponse;
    }
    
    @RequestMapping("deleteUserArea")
    @ResponseBody
    public AjaxResponse<UserAreaDTO> deleteUserArea(Long id){
        Integer row = userareaServiceImpl.deleteUserAreaById(id);
        AjaxResponse<UserAreaDTO> ajaxResponse = null;
        
        if(row > 0){
            ajaxResponse = new AjaxResponse<UserAreaDTO>(AjaxError.DELETEADDRESSCOMMITSUCCESS.getCode(),AjaxError.DELETEADDRESSCOMMITSUCCESS.getMessage());
        }else{
            ajaxResponse = new AjaxResponse<UserAreaDTO>(AjaxError.DELETADDERESSCOMMITFAIL.getCode(),AjaxError.DELETADDERESSCOMMITFAIL.getMessage());
        }
        return ajaxResponse;
    }
    
    
}